package com.c.ctgproject.ui.collect.check

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CheckOutListAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityOutCheckBinding
import com.c.ctgproject.entity.guang.OutCheckEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe

/**
 *作者:LiBW
 *创建日期: 2021/10/12
 *描述: 出库审批 页面
 */
class OutCheckActivity : BaseActivityV2(true) {
    private val mBinding by lazy { ActivityOutCheckBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private val mList = ArrayList<OutCheckEntity.DataBean.ListBean>()
    private val mAdapter by lazy { CheckOutListAdapter(this, mList) }

    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1) {
            mList.removeAt(type.position)
            mAdapter.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mRecyclerview.adapter = mAdapter
        mBinding.mRecyclerview.layoutManager = LinearLayoutManager(this)
    }

    private var search: String = ""


    override fun click() {
        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }


        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "待审批" -> {
                    statue = 1
                    page = 1
                    getData()
                }
                "已审批" -> {
                    statue = 2
                    page = 1
                    getData()
                }
            }
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    //出库单状态 0-新建 1-待审批 2-已审批
    private var statue = 1
    private fun getData() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.outCheckListAll(2, search, page, statue), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("出库审批列表:$it")
            val data = getGson().fromJson(it, OutCheckEntity::class.java).data
            data?.list?.let { list ->
                if (page == 1) mList.clear()
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mRecyclerview.handlerPosition(page)
            }
        })
    }
}